package com.exp.practice.workbench.controller.teacher;


import com.exp.practice.base.exception.MyException;
import com.exp.practice.base.utils.UUIDUtils;
import com.exp.practice.base.vo.PageVo;
import com.exp.practice.base.vo.ResultVo;
import com.exp.practice.workbench.domain.Examination;
import com.exp.practice.workbench.domain.Question;
import com.exp.practice.workbench.service.teacher.TeacherMakeTestService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("workbench/teacher/makeTest")
public class TeacherMakeTestController {

    @Resource
    private TeacherMakeTestService teacherMakeTestService;


    @RequestMapping("/pageList.do")
    @ResponseBody
    public PageVo pageList(String pageNo, String pageSize, String []qid)  {
        PageVo pageVo=new PageVo();
        //得到总题目数
        int total=qid.length;
        //得到所查询数据
        List<Question> list=teacherMakeTestService.getQuestions(pageNo,pageSize,qid);
        pageVo.setTotal(total);
        pageVo.setDataList(list);
        return pageVo;
    }



    @RequestMapping("/addExamination.do")
    @ResponseBody
    public ResultVo addExamination(Examination examination,String []qid)  {
        ResultVo resultVo=new ResultVo();
        //设置id
        examination.setEid(UUIDUtils.getUUID());
        try {
           teacherMakeTestService.addExamination(examination,qid);
           resultVo.setSuccess(true);
           resultVo.setMsg("生成试卷成功");
        } catch (MyException e) {
            resultVo.setMsg(e.getMessage());
            resultVo.setSuccess(false);
        }
        return resultVo;
    }

    @RequestMapping("/getExaminationList.do")
    @ResponseBody
    public PageVo getExaminationList(String pageNo, String pageSize,Examination examination)  {
        PageVo pageVo=new PageVo();
        int total=teacherMakeTestService.getTotal(examination);
        List<Examination> list=teacherMakeTestService.getExaminationList(pageNo,pageSize,examination);
        pageVo.setTotal(total);
        pageVo.setDataList(list);
        return pageVo;
    }

    @RequestMapping("/publish.do")
    @ResponseBody
    public ResultVo publish(String eid)  {
        ResultVo resultVo=new ResultVo();
        try {
            teacherMakeTestService.publish(eid);
            resultVo.setSuccess(true);
            resultVo.setMsg("发布成功");
        } catch (MyException e) {
            resultVo.setMsg(e.getMessage());
            resultVo.setSuccess(false);
        }
        return resultVo;
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public ResultVo delete(String[] eid){
        ResultVo resultVo=new ResultVo();
        try {
            int count=teacherMakeTestService.delete(eid);
            resultVo.setSuccess(true);
            resultVo.setMsg("删除成功");
        } catch (MyException e) {
            resultVo.setSuccess(false);
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

    @RequestMapping("/getConcreteExamination.do")
    public ModelAndView doTest(String eid){
        Map<String,Object> map=new HashMap<>();
        ModelAndView modelAndView=new ModelAndView();
        map=teacherMakeTestService.getConcreteExamination(eid);
        modelAndView.addObject("examination",map.get("examination"));
        modelAndView.addObject("questionList",map.get("questionList"));
        modelAndView.setViewName("workbench/teacher/makeTest/detail");
        return modelAndView;
    }

}
